package com.hbx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbx.auth.AuthCacheService;
import com.hbx.dto.comment.InsertCommentDto;
import com.hbx.exception.RspException;
import com.hbx.model.Comment;
import com.hbx.mapper.CommentMapper;
import com.hbx.model.User;
import com.hbx.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbx.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hbx
 * @since 2022-01-15
 */
@Service
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public void insertComment(InsertCommentDto dto, Result r) {
        User user = AuthCacheService.getUser();
        Comment comment = new Comment();
        BeanUtils.copyProperties(dto, comment);
        comment.setCommentDate(new Date());
        comment.setCommentWid(user.getUserId());
        int insert = commentMapper.insert(comment);
        if (insert != 1) {
            throw new RspException(201, "评论添加失败");
        }
        r.setCode(200).setMsg("评论成功");
    }

    /**
     * 删除评论
     *
     * @param commentId
     * @param r
     */
    @Override
    @Transactional
    public void removeComment(Integer commentId, Result r) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null) {
            r.setCode(201).setMsg("评论不存在");
            log.error("【评论模块】:评论不存在,删除失败,{}", commentId);
            return;
        }
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_comment_id", commentId);
        // 获得所有直接子评论
        List<Comment> commentList = commentMapper.selectList(wrapper);
        int result = commentMapper.deleteById(commentId);
        if (result != 1){
            throw new RspException("删除失败");
        }
        int count = 0;
        log.info("【递归开始】:count={}", count);
        recursionRemoveComment(r, commentList, count);
    }

    /**
     * 递归删除评论
     *
     * @param r
     * @param commentList
     * @param c
     */
    private void recursionRemoveComment(Result r, List<Comment> commentList, Integer c) {
        if (CollectionUtils.isEmpty(commentList)) {
            r.setCode(200).setMsg("删除成功");
            return;
        }
        log.info("【评论模块】:已经递归{}次", c);
        c++;
        List<Integer> ids = commentList.stream().map(Comment::getCommentId).collect(Collectors.toList());
        int count = commentMapper.deleteBatchIds(ids);
        if (count != ids.size()) {
            throw new RspException("删除失败");
        }
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.in("parent_comment_id", ids);
        List<Comment> comments = commentMapper.selectList(wrapper);
        recursionRemoveComment(r, comments, c);
    }
}
